﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using BEGDC.Utility.Castle;
using BEGDC.Utility.ExceptionManagement;
using BEGDC.CC.BL.Common.Query;
using BEGDC.CC.BL.Workflow;
using BEGDC.CC.BL.UserManagement;
using BEGDC.CC.BL.Common;
using BEGDC.CC.Entity;
using BEGDC.CC.Web.UserControls;
using NetBpm.Util.Client;
using NetBpm.Workflow.Execution;
using NetBpm.Workflow.Execution.EComp;

namespace BEGDC.CC.Web.SystemManagement
{
	/// <summary>
	/// Summary description for main.
	/// </summary>
	public class Hotel : System.Web.UI.Page
	{
		/// <summary>
		/// 
		/// </summary>
		private string _Sorting
		{
			get
			{
				if( ViewState["Sorting"] == null ) return string.Empty;

				return ViewState["Sorting"].ToString(); 
			}
			set{ ViewState["Sorting"]=value; }
		}


		protected System.Web.UI.WebControls.Label lblRecordCount;
		protected System.Web.UI.WebControls.Label lblCurrentPageIndex;
		protected System.Web.UI.WebControls.Label lblPageCount;
		protected System.Web.UI.WebControls.ImageButton btnPageFirst;
		protected System.Web.UI.WebControls.ImageButton btnPagePrev;
		protected System.Web.UI.WebControls.ImageButton btnPageNext;
		protected System.Web.UI.WebControls.ImageButton btnPageLast;
		protected System.Web.UI.WebControls.TextBox txtPage;
		protected System.Web.UI.WebControls.ImageButton btnGotoPage;

		protected System.Web.UI.WebControls.DataGrid dgHotel;

		protected System.Web.UI.WebControls.ImageButton btnNewHotel;

		protected System.Web.UI.WebControls.Label lbAddHotel;
		protected System.Web.UI.WebControls.Label lbHotelName;
		
		protected System.Web.UI.WebControls.Label lbDescription;
		protected System.Web.UI.WebControls.TextBox txtNewLegalName;
		protected System.Web.UI.WebControls.ImageButton btnNewType;
		protected System.Web.UI.HtmlControls.HtmlGenericControl span2;
		protected System.Web.UI.WebControls.CheckBox chkEnabled;
		protected System.Web.UI.WebControls.DropDownList ddlLocation;
		protected System.Web.UI.WebControls.ImageButton btnExport;

		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!Page.IsPostBack)
			{
				InitializeGlobalization();
				BindHotel(1);
			}
			else
			{
				this.BindHotel(this._CurrentPageIndex);
			}
		}

		private void InitializeGlobalization()
		{
			this.btnNewHotel.ImageUrl = "../images/b-Add.gif";

		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.dgHotel.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dgHotel_SortCommand);
			this.dgHotel.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgHotel_ItemDataBound);
			this.btnPageFirst.Click += new System.Web.UI.ImageClickEventHandler(this.btnPageFirst_Click);
			this.btnPagePrev.Click += new System.Web.UI.ImageClickEventHandler(this.btnPagePrev_Click);
			this.btnPageNext.Click += new System.Web.UI.ImageClickEventHandler(this.btnPageNext_Click);
			this.btnPageLast.Click += new System.Web.UI.ImageClickEventHandler(this.btnPageLast_Click);
			this.btnGotoPage.Click += new System.Web.UI.ImageClickEventHandler(this.btnGotoPage_Click);
			this.btnNewHotel.Click += new System.Web.UI.ImageClickEventHandler(this.btnNewHotel_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		#region ---------- paging events ----------------
		private void btnPageFirst_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			this.dgHotel.CurrentPageIndex = 0;
			this.BindHotel(1);
		}

		private void btnPagePrev_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			if( _CurrentPageIndex == 1 ) return;
			this.dgHotel.CurrentPageIndex = _CurrentPageIndex-2;
			this.BindHotel(_CurrentPageIndex - 1);
		}

		private void btnPageNext_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			if( _CurrentPageIndex == _PageCount ) return;	
			this.dgHotel.CurrentPageIndex = _CurrentPageIndex;
			this.BindHotel( _CurrentPageIndex + 1 );
		}

		private void btnPageLast_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			this.dgHotel.CurrentPageIndex = _PageCount-1;
			this.BindHotel( _PageCount );
		}

		private void btnGotoPage_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			if (this.txtPage.Text == string.Empty)	
			{
				this.txtPage.Text = "1";
			}
			int gotopage = 1;
			try
			{
				gotopage = Convert.ToInt32(this.txtPage.Text);
			}
			catch{}
			int currentpage = 0;

			if (gotopage >= _PageCount)	
			{
				gotopage = _PageCount;
				currentpage = gotopage - 1;
				if (currentpage < 0) currentpage = 0;
			}
			else if (gotopage <= 0)
			{
				gotopage = 1;
			}
			else
			{
				currentpage = gotopage -1;
			}

			this.dgHotel.CurrentPageIndex = currentpage;

			this.BindHotel( gotopage );

			this.txtPage.Text = string.Empty;
		}

		#endregion

		#region page navigation
		private int _PageCount
		{
			get
			{
				if( ViewState["PageCount"] != null ) return (int) ViewState["PageCount"];
				else
					return 0;
			}
			set
			{
				ViewState["PageCount"] = value;
			}
		}
		private int _CurrentPageIndex
		{
			get
			{
				if( ViewState["CurrentPageIndex"] != null ) return (int) ViewState["CurrentPageIndex"];
				else
					return 0;
			}
			set
			{
				ViewState["CurrentPageIndex"] = value;
			}
		}
		private int _RowCount
		{
			get
			{
				if( ViewState["RowCount"] != null ) return (int) ViewState["RowCount"];
				else
					return 0;
			}
			set
			{
				ViewState["RowCount"] = value;
			}
		}
		private void SetPageNavi( int newPage )
		{
			if( newPage <= 1 )
			{
				this.btnPageFirst.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/pg_first_inact.gif";
				this.btnPagePrev.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/pg_pre_inact.gif";
				this.btnPageFirst.Attributes["disabled"] = "true";
				this.btnPagePrev.Attributes["disabled"] = "true";
			}
			else
			{
				this.btnPageFirst.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/pg_first.gif";
				this.btnPagePrev.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/pg_pre.gif";
				this.btnPageFirst.Attributes.Remove("disabled");
				this.btnPagePrev.Attributes.Remove("disabled");
			}

			if( newPage >= _PageCount )
			{
				this.btnPageNext.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/pg_next_inact.gif";
				this.btnPageLast.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/pg_last_inact.gif";
				this.btnPageNext.Attributes["disabled"] = "true";
				this.btnPageLast.Attributes["disabled"] = "true";
			}
			else
			{
				this.btnPageNext.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/pg_next.gif";
				this.btnPageLast.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/pg_last.gif";
				this.btnPageNext.Attributes.Remove("disabled");
				this.btnPageLast.Attributes.Remove("disabled");
			}

			this.lblCurrentPageIndex.Text = newPage.ToString();
			this.lblPageCount.Text = _PageCount.ToString();
			this.lblRecordCount.Text = _RowCount.ToString();

		}

		private void SetPageCount(int count, int pageIndex)
		{
			if( count > 0 )
			{
				_RowCount = count;
				_PageCount = count / this.dgHotel.PageSize;
				if( count % this.dgHotel.PageSize > 0) _PageCount ++;

				_CurrentPageIndex = pageIndex;	
				SetPageNavi( pageIndex );
			}	
			else
			{
				_RowCount = count;
				_PageCount = count;
				_CurrentPageIndex = 0;
				SetPageNavi( 0 );
			}
		}

		#endregion

		private void BindHotel(int pageIndex)
		{
//			IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
//
//			//System.Data.DataTable allHotel = utility.GetHotelDataTable();
//
//			if (this._Sorting != string.Empty)
//			{
//				allHotel.DefaultView.Sort = this._Sorting;
//			}
//
//			this.dgHotel.DataSource = allHotel.DefaultView;
//			this.dgHotel.DataBind();
//
//
//			int count = allHotel.DefaultView.Count;
//			SetPageCount(count, pageIndex);

		}

		private void InitializeDropDownList()
		{
			//Department
			IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as  IUtility;
			this.ddlLocation.DataSource = utility.GetAllDicItem(BEGDC.CC.BL.Common.DicType.HOTELLOCATION);
			this.ddlLocation.DataValueField = "ID";
			this.ddlLocation.DataTextField = "DicItemKey";
			this.ddlLocation.DataBind();
		}

		private void btnNewHotel_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
//			if(this.txtNewLegalName.Text.Trim() == string.Empty)
//			{
//				Page.RegisterClientScriptBlock("","<script>alert('Please input hotel entity name!');</script>");
//				return;
//			}
//			BEGDC.CC.Entity.Hotel hotel = new BEGDC.CC.Entity.Hotel();
//
//			IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
//			int result = utility.AddHotel(hotel);
//			if(result == -1)
//				Page.RegisterClientScriptBlock("","<script>alert('Data hotel entity is already exist!');</script>");
//			else if(result == 0)
//				Page.RegisterClientScriptBlock("","<script>alert('Add data hotel entity failed!');</script>");
//			else
//			{
//				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');</script>");
//				CCIdentity id = this.User.Identity as CCIdentity;
//				BEGDC.CC.Entity.OperationLog log = new BEGDC.CC.Entity.OperationLog();
//				log.Module = "DataManagement";
//				Employee emp = new Employee();
//				emp.Id = id.UserInfo.Id;
//				log.Employee = emp;
//				log.Time = System.DateTime.Now;
//				log.Description = "Add new data hotel entity.";
//				utility.SaveOperationLog(log);
//				this.BindHotel(1);
//			}
		}
		private void EditHotel_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			this.BindHotel(this._CurrentPageIndex);
		}

		private void dgHotel_ItemDataBound(object source, System.Web.UI.WebControls.DataGridItemEventArgs e)
		{
			try
			{
				if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
				{
					string hotel = e.Item.Cells[0].Text;
					ImageButton ibhotel = (ImageButton)e.Item.FindControl("imageLegal");
					ibhotel.Click +=new System.Web.UI.ImageClickEventHandler(this.EditHotel_Click);
					ibhotel.Attributes.Add("onclick","javascript:showLegalForm("+hotel+");");
				}
			}
			catch(GDCException gdcEx)
			{
				WebUtility.Go2ErrorPage(this.Page, gdcEx.ErrorNumber);
			}
			catch(Exception ex)
			{
				WebUtility.Go2ErrorPage(this.Page, BEGDC.CC.BL.Common.ErrorCode.Other_Exception);
			}
		}

		public void dgHotel_SortCommand(object source, DataGridSortCommandEventArgs e)
		{
			if (this._Sorting.IndexOf(e.SortExpression) > -1 && this._Sorting == e.SortExpression)
				this._Sorting = e.SortExpression + " desc";
			else
				this._Sorting = e.SortExpression;
			this.BindHotel(this._CurrentPageIndex);
			
		}

	}
}
